﻿using System;
using System.Diagnostics;
using System.IO;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Security;
using DAO.Cdc;

namespace CdcBusiness
{
    public class PdfDocumentGenerator
    {
        public static PdfDocument PdfCreate()
        {
            string filename = Guid.NewGuid().ToString().ToUpper() + ".pdf";
            //create a pdf document 
            PdfDocument document = new PdfDocument(filename);
            //create a pdf page 
            PdfPage page = document.AddPage();
            //required for writing anything to a pdf 
            XGraphics gfx = XGraphics.FromPdfPage(page);

            //Draw the PDF 
            XFont fontH1 = new XFont("Times New Roman", 30, XFontStyle.Bold);
            gfx.DrawString("PDF TEST FILE", fontH1, XBrushes.DarkBlue, 123, 50);

            //y = DrawHeader(gfx, tf, rect, y); 
            //y = DrawContactBox(gfx, tf, rect, y); 
            //y = DrawVendortBox(gfx, tf, rect, y); 
            //y = DrawCustomerBox(gfx, tf, rect, y); 
            //y = DrawJobInfoBox(gfx, tf, rect, y); 
            //y = DrawConditionsBox(gfx, tf, rect, y); 
            //y = DrawServiceBox(gfx, tf, rect, y); 
            //y = DrawInstructionsBox(gfx, tf, rect, y); 

            // Save the document 
            //document.Save(filename); // YOU DO NOT NEED THIS LINE ... IT AUTO SAVES ON THE CLOSE EVENT 
            document.Close(); // doesn't seem to work 
            return document;
        }

        public static PdfDocument generate1stPage(String dateTVA, String moreInfo)
        {
            String shortDateString = "le " + DateTime.Now.ToShortDateString();
            String name = "Cédric DAGORNE";

            // Create a new PDF document
            PdfDocument document = new PdfDocument("toto.pdf");
            document.Info.Title = "Created By Cédric DAGORNE";

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Create a font
            XFont fontTitle = new XFont("Times New Roman", 32, XFontStyle.Bold);
            XBrush brushTitle = new XSolidBrush(XColor.FromArgb(33, 59, 117));
            XFont fontSubTitle = new XFont("Times New Roman", 15, XFontStyle.Bold);
            XBrush brushSubTitle = new XSolidBrush(XColor.FromArgb(39, 87, 165));
            XBrush brushFootTitle = new XSolidBrush(XColor.FromArgb(153, 204, 255));


            XFont fontInfos = new XFont("Times New Roman", 13, XFontStyle.Bold);
            XBrush brushInfos = new XSolidBrush(XColor.FromArgb(255, 0, 0));

            // Draw the text
            gfx.DrawString("Cédric DAGORNE Consulting", fontTitle, brushTitle,
              new XRect(0, 0, page.Width, page.Height - 100),
              XStringFormats.Center);

            // Draw the text
            if (!String.IsNullOrEmpty(dateTVA))
                gfx.DrawString(dateTVA, fontSubTitle, brushSubTitle,
                  new XRect(0, 10, page.Width, page.Height - 50),
                  XStringFormats.Center);


            if (!String.IsNullOrEmpty(moreInfo))
            {
                String[] infos = moreInfo.Split('\n');
                int i = 0;
                foreach (String info in infos)
                {
                    gfx.DrawString("\xB7\t" + info, fontInfos, brushInfos,
                      new XRect(60, (3 * page.Height / 5) + i * 20, page.Width, 50),
                      XStringFormats.TopLeft);
                    i++;
                }
            }

            // Draw the text
            gfx.DrawString(name, fontSubTitle, brushFootTitle,
              new XRect(10, page.Height - 150, 200, 150),
              XStringFormats.Center);

            // Draw the text
            gfx.DrawString(shortDateString, fontSubTitle, brushFootTitle,
              new XRect(page.Width - 200, page.Height - 150, 150, 150),
              XStringFormats.Center);

            return document;
        }



        public static PdfDocument selectFile(String filePath)
        {
            return PdfReader.Open(filePath, PdfDocumentOpenMode.Import);
        }

        public static PdfDocument getFileToImport(Stream stream)
        {
            return PdfReader.Open(stream, PdfDocumentOpenMode.Import);
        }

        public static PdfDocument getFileToSave(Stream stream)
        {
            return getFile(stream, PdfDocumentOpenMode.Modify);
        }

        private static PdfDocument getFile(Stream stream, PdfDocumentOpenMode openMode)
        {
            return PdfReader.Open(stream, openMode);
        }

        public static PdfDocument getFile(PdfDocument document, int from, int to)
        {
            PdfDocument _return = new PdfDocument();
            for (int i = from; i <= to; i++)
            {
                _return.AddPage(document.Pages[i - 1]);
            }
            return _return;
        }
    }
}
